package cn.core.sap.modules.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.core.sap.dao.mapper.Sap_jcodatasourceMapper;
import cn.core.sap.dao.po.Sap_jcodatasourcePO;
import cn.core.sap.modules.service.Sap_jcodatasourceService;
import cn.osworks.aos.core.asset.AOSJson;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.asset.WebCxt;
import cn.osworks.aos.core.dao.SqlDao;
import cn.osworks.aos.core.excel.POIExcelUtil;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;
import cn.osworks.aos.core.typewrap.impl.HashDto;

/**
 * <b>SAP数据源[sap_jcodatasource] controller</b>
 * <p>
 * </p>
 * 
 * @author penghe
 * @date 2016-08-17 15:45:36
 */
@Controller
@RequestMapping(value = "cor/sap_jcodatasource")
public class Sap_jcodatasourceController {
	@Autowired
	private Sap_jcodatasourceService sap_jcodatasourceservice;
	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private Sap_jcodatasourceMapper sap_jcodatasourcemapper;
	
    /**
	 * SAP数据源 页面初始化
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "init")
	public String init(HttpSession session, HttpServletRequest request) {
		return "core/sap/sap_jcodatasource.jsp";
	}

	/**
	 * 查询SAP数据源
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "listSap_jcodatasource")
	public void listSap_jcodatasource(HttpServletRequest request, HttpServletResponse response) {
		Dto inDto = Dtos.newDto(request);
		HttpSession session = request.getSession();
		if (session != null)
			session.setAttribute("sap_jcodatasourcesessionid", inDto);
		//sql字定义查询 
	    //List<Sap_jcodatasourcePO> list = sqlDao.list("Sap_jcodatasource.listSap_jcodatasource", inDto);
		//标准查询 
		List<Sap_jcodatasourcePO> list = sap_jcodatasourcemapper.likePage(inDto);
		String outString = AOSJson.toGridJson(list, inDto.getPageTotal());
		WebCxt.write(response, outString);
	}
	/**
	 * 保存SAP数据源
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveSap_jcodatasource")
	public void saveSap_jcodatasource(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		                                 		 dto.put("crea_date_", AOSUtils.getDateTimeStr());
		    		 dto.put("account_", dto.getUserInfo().getAccount_());
		    		 dto.put("name_", dto.getUserInfo().getName_());
		   		Dto outDto =sap_jcodatasourceservice.saveSap_jcodatasource(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	/**
	 * 更新SAP数据源
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateSap_jcodatasource")
	public void updateSap_jcodatasource(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		                                 		 dto.put("crea_date_", AOSUtils.getDateTimeStr());
		    		 dto.put("account_", dto.getUserInfo().getAccount_());
		    		 dto.put("name_", dto.getUserInfo().getName_());
		   		Dto outDto =sap_jcodatasourceservice.updateSap_jcodatasource(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	/**
	 * 删除SAP数据源
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteSap_jcodatasource")
	public void deleteSap_jcodatasource(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto = sap_jcodatasourceservice.delete(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	/**
	 * 导出SAP数据源
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exportexcel")
	public void exportexcelSap_jcodatasource(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
	    Dto dto = (HashDto) session.getAttribute("sap_jcodatasourcesessionid");
		String filename="";
		try {
	    filename = URLEncoder.encode("sap_jcodatasource", "utf-8");
		InputStream bis = null;
		BufferedInputStream buffis = null;
		OutputStream fos = null;
		BufferedOutputStream bos = null;
		fos = response.getOutputStream();
		bos = new BufferedOutputStream(fos);
		String[] titleHead=new String[] { "ID", "数据源ID", "客户端", "应用服务器", "系统ID", "系统编号", "语言", "用户", "密码", "消息服务器", "登录组", "缓冲池连接数", "峰值数量", "sap 路由", "启用状态", "创建时间", "操作帐号", "操作人姓名", };
		String[] datarow=new String[]   { "id_", "pk_", "client_", "host_", "sysid_", "sysno_", "lang_", "user_", "pass_", "mhost_", "sapgroup_", "pool_capacity_", "peak_limit_", "saprouter_", "enabled_", "crea_date_", "account_", "name_", };
		List<Sap_jcodatasourcePO> list = sap_jcodatasourcemapper.likePage(dto);
		List<Dto> outlist = new ArrayList<Dto>();
		for(int i=0;i<list.size();i++){
			Sap_jcodatasourcePO sap_jcodatasourcePO =  list.get(i);
			Dto dataDto = new HashDto();
			AOSUtils.copyProperties(sap_jcodatasourcePO, dataDto);
			outlist.add(dataDto);
		}
		bis = POIExcelUtil.create(filename, outlist,titleHead , datarow); 
		buffis = new BufferedInputStream(bis);
		response.setContentType("application/xls");
		response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename + ".xls", "utf-8"));
		int bytesRead = 0;
		byte[] buffer = new byte[1024];
		while ((bytesRead = buffis.read(buffer, 0, buffer.length)) != -1) {
			bos.write(buffer, 0, bytesRead);
		}
		bos.flush();
		bis.close();
		buffis.close();
		fos.flush();
		fos.close();
		bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
